import type { DataNode } from 'ant-design-vue/es/tree'
import { isEmpty } from 'lodash-es'
import { omit } from 'xe-utils'

//# 变量

const TREE_DATA = genTreeData(4, 15)
// const TREE_DATA = genTreeData(5, 5)

//# 类型

type TreeDataMap = {
  pkey2children: Entity<TreeNode[]>
  key2node: Entity<TreeNode>
}

//# 定义

function genTreeData(level: number, size: number, levelIndex = 0, pkey?: string) {
  const treeData: DataNode[] = []
  for (let i = 0; i < size; i++) {
    const key = [pkey, `${i + 1}`].filter(Boolean).join('-')
    const node = {
      title: `Node ${key}`,
      key,
    } as DataNode

    if (['Node 1-1', 'Node 1-2', 'Node 2-2'].includes(node.title)) {
      node.title = 'Darwin'
    }

    treeData.push(node)

    if (level > 1 && levelIndex + 1 !== level) {
      node.children = genTreeData(level, size, levelIndex + 1, key)
    }
  }
  return treeData
}

function formatTreeData(treeData: DataNode[]) {
  const map: TreeDataMap = {
    pkey2children: {},
    key2node: {},
  }
  const tree = treeMap(treeData, (node, pnode, { index, level }) => {
    const pkey = pnode?.key ?? '0'
    const data = { ...node, level, pkey, pos: index } as TreeNode
    const cnode = omit(data, ['children', 'pos']) as TreeNode
    ;(
      !map.pkey2children[pkey] ? map.pkey2children[pkey] = [] : map.pkey2children[pkey]
    ).push(cnode)
    map.key2node[node.key] = cnode
    return data
  })
  return { treeData: tree, treeDataMap: map }
}

function filterTreeData(treeDataMap: TreeDataMap, filter: (node: TreeNode) => boolean) {
  const nodeList: TreeNode[] = []
  Object.values(treeDataMap.pkey2children).forEach((nodes) => {
    nodeList.push(...nodes.filter(filter))
  })
  console.log(nodeList)

  /** 节点映射 */
  const nodeMap: Entity<TreeNode> = {}
  /** 一级节点 */
  const level1Nodes: TreeNode[] = []
  /** 展开节点 */
  const expandedKeys: Key[] = []

  /* 组装树节点 */
  //? 由查询出的节点，向上恢复成树结构
  nodeList.forEach((node) => {
    wrapNode(node)
  })

  /* 延展树节点 */
  //? 由查询出的节点的最末节点，向下延展原有的树结构
  //? 并且检索出展开节点
  Object.values(nodeMap).forEach((node) => {
    /* 展开节点 */
    if (node.children) {
      expandedKeys.push(node.key)
    }
    /* 末级节点 */
    else {
      extendNode(node)
    }
  })

  console.log(nodeMap, level1Nodes)

  return { treeData: level1Nodes, expandedKeys }

  //# 方法

  function wrapNode(node: TreeNode) {
    let cnode = nodeMap[node.key]
    if (!cnode) {
      cnode = nodeMap[node.key] = { ...node }
    }
    /* 根节点 */
    if (cnode.pkey === '0') {
      if (!level1Nodes.includes(cnode)) {
        level1Nodes.push(cnode)
      }
    }
    /* 非根节点 */
    else {
      let pnode = nodeMap[cnode.pkey!]
      if (!pnode) {
        pnode = nodeMap[cnode.pkey!] = { ...treeDataMap.key2node[cnode.pkey!] }
      }
      if (!pnode.children) {
        pnode.children = []
      }
      if (!pnode.children.includes(cnode)) {
        pnode.children.push(cnode)
      }
      wrapNode(pnode)
    }
  }

  function extendNode(node: TreeNode) {
    const children = treeDataMap.pkey2children[node.key]
    if (!isEmpty(children)) {
      node.children = children.map(cnode => extendNode({ ...cnode }))
    }
    return node
  }
}

//# 导出

export type { TreeDataMap }
export { TREE_DATA, formatTreeData, filterTreeData }
